from sklearn import datasets
from sklearn.linear_model import LinearRegression
import numpy as np
import matplotlib.font_manager as fm
from matplotlib import rcParams
import matplotlib.pyplot as plt
from collections import OrderedDict
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']

# def import_data(data_path:str):
#     pass
#
# #X =np.array ([0, 10, 20, 30, 40, 50, 60, 70])
# #y = np.array([25.35, 27.35, 29.35, 31.30, 33.40, 35.35, 37.40, 39.30])

# 从 EXCEL 读入 砝码重量 和 弹簧长度
# df = pd.read_excel("data.xlsx", usecols=[0, 1])
df = pd.read_csv("data.csv")
y = df.loc[:, 'weight'].values   # 砝码重量
X = df.loc[:, 'length'].values   # 弹簧长度

plt.figure(figsize=(12, 9))
plt.xlim(24, 41)
plt.scatter(X, y, 100)
plt.xlabel('弹簧长度（cm）', fontsize=18)
plt.ylabel('砝码重量（g）', fontsize=18)
plt.title('胡克定律实验', fontsize=20)
plt.show()

X_train = X.reshape(-1, 1)       # 转换为训练数据
y_train = y.reshape(-1, 1)       # 转换为训练数据

model = LinearRegression()       # 定义线性回归模型
model.fit(X_train, y_train)      # 训练数据

# 打印训练结果
print("y=", model.coef_[0][0], "x+", model.intercept_[0])


x = model.predict([[80]])
print("x=", x)
#plt.scatter(X_train, y_train, label="训练标签")
#y_train_pred = model.predict(X_train)
#plt.plot(X_train,y_train_pred,color="green",linewidth=3,label="最佳拟合曲线")
#plt.show()